package com.weetop.akly.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.weetop.akly.R;
import com.weetop.akly.activity.communicationIndex.UserDetailActivity;
import com.weetop.akly.activity.meessage.SignDayRecordActivity;
import com.weetop.akly.appConfig.GlobalConstants;
import com.weetop.akly.base.BaseFragment;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.AccountInfoBean;
import com.weetop.akly.presenter.AccountInfoPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.GlideUtil;
import com.weetop.akly.view.AccountView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MineFragment extends BaseFragment<AccountInfoPresent> implements AccountView {
    @BindView(R.id.iv_head)
    ImageView ivHead;
    Unbinder unbinder;
    @BindView(R.id.iv_salaryInfo)
    ImageView ivSalaryInfo;
    @BindView(R.id.rl_applylist)
    RelativeLayout rlApplylist;
    @BindView(R.id.rl_recordList)
    RelativeLayout rlRecordList;
    @BindView(R.id.rl_setting)
    RelativeLayout rlSetting;
    @BindView(R.id.tv_tomine)
    TextView tvTomine;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.te_departName)
    TextView teDepartName;
    @BindView(R.id.rl_votecenter)
    RelativeLayout rlVotecenter;
    @BindView(R.id.rl_train)
    RelativeLayout rlTrain;
    private AccountInfoBean accountInfoBean;

    @Override
    protected AccountInfoPresent createPresenter() {
        return new AccountInfoPresent(this, getActivity());

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_mine, null);
        unbinder = ButterKnife.bind(this, view);

        initData();
        //获取个人信息
        mPresenter.getAccountInfo();
        return view;
    }

    private void initData() {
        if (myApplication.getAccountInfoBean() != null) {
            accountInfoBean = myApplication.getAccountInfoBean();
            tvName.setText(accountInfoBean.getInfo().getName());
            teDepartName.setText(accountInfoBean.getInfo().getDepartment());
//            GlideUtil.load(mActivity, ivHead, accountInfoBean.getInfo().getPhoto(), R.mipmap.headimg, R.mipmap.headimg);

            Glide.with(mActivity)
                    .load(accountInfoBean.getInfo().getPhoto())
                    .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                    .apply(new RequestOptions()
                            .transforms(new CenterCrop(), new RoundedCorners(15))
                            .placeholder(R.mipmap.headimg)//图片加载出来前，显示的图片
                            .fallback(R.mipmap.headimg)
                            .error(R.mipmap.headimg))
                    .into(ivHead);
        }
    }

    /**
     * 获取个人信息成功
     *
     * @param o
     */
    @Override
    public void getAccountInfoSuccess(BaseModel<AccountInfoBean> o) {
        accountInfoBean = o.getData();
        tvName.setText(accountInfoBean.getInfo().getName());
        teDepartName.setText(accountInfoBean.getInfo().getDepartment());
//        GlideUtil.load(mActivity, ivHead, accountInfoBean.getInfo().getPhoto(), R.mipmap.headimg, R.mipmap.headimg);
        Glide.with(mActivity)
                .load(accountInfoBean.getInfo().getPhoto())
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .apply(new RequestOptions()
                        .transforms(new CenterCrop(), new RoundedCorners(15))
                        .placeholder(R.mipmap.headimg)//图片加载出来前，显示的图片
                        .fallback(R.mipmap.headimg)
                        .error(R.mipmap.headimg))
                .into(ivHead);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_applylist, R.id.rl_approvelist, R.id.rl_recordList, R.id.rl_setting, R.id.rl_votecenter, R.id.rl_train})
    public void onViewClicked(View view) {
        if (BaseUtils.fastClick())
            switch (view.getId()) {
                case R.id.rl_applylist:
                    BaseUtils.toActivity(mActivity, AlpplyListActivity.class,"1");
                    break;
                case R.id.rl_approvelist:
                    BaseUtils.toActivity(mActivity, AlpplyListActivity.class,"2");
                    break;
                case R.id.rl_votecenter:
                    BaseUtils.toActivity(mActivity, VoteCenterActivity.class);
                    break;
                case R.id.rl_train:
                    BaseUtils.toActivity(mActivity, TrainListActivity.class);
                    break;
                case R.id.rl_recordList:
                    BaseUtils.toActivity(mActivity, SignDayRecordActivity.class);
                    break;
                case R.id.rl_setting:
                    BaseUtils.toActivity(mActivity, SettingActivity.class);
                    break;
            }
    }

    @OnClick({R.id.tv_tomine, R.id.iv_salaryInfo})
    public void toactivity(View view) {
        if (BaseUtils.fastClick())
            switch (view.getId()) {
                case R.id.tv_tomine:
                    Intent intent = new Intent();
                    intent.putExtra(GlobalConstants.Account, accountInfoBean.getAccount().getAccount());
                    intent.putExtra(GlobalConstants.Name, accountInfoBean.getInfo().getName());
                    intent.putExtra(GlobalConstants.CompanyTel, accountInfoBean.getAccount().getCompany_tel());
                    intent.putExtra(GlobalConstants.Email, accountInfoBean.getAccount().getEmaill_account());
                    intent.putExtra(GlobalConstants.HeadPhoto, accountInfoBean.getInfo().getPhoto());
                    intent.putExtra(GlobalConstants.Job, accountInfoBean.getInfo().getJob());
                    intent.putExtra(GlobalConstants.Department, accountInfoBean.getInfo().getDepartment());
                    intent.putExtra(GlobalConstants.Telephone, accountInfoBean.getInfo().getTelephone());
                    intent.putExtra(GlobalConstants.TargetId, accountInfoBean.getInfo().getId());
                    BaseUtils.toActivity(mActivity, UserDetailActivity.class, intent);
                    break;
                case R.id.iv_salaryInfo:
                    BaseUtils.toActivity(mActivity, SalaryActivity.class);
                    break;
            }
    }

//    @OnClick(R.id.iv_salaryInfo)
//    public void onViewClicked() {
//        if (BaseUtils.fastClick())
//            BaseUtils.toActivity(mActivity, SalaryActivity.class);
//    }
}